mod todo;
mod cli;
mod storage;

use cli::{add_todo, list_todos, toggle_todo};
use storage::{load_todos, save_todos};
use std::io;

fn main() {
    let mut todos = load_todos().unwrap_or_default();

    loop {
        println!("Please input option (add/list/toggle/exit): ");
        let mut input = String::new();
        io::stdin().read_line(&mut input).unwrap();

        let input = input.trim();

        match input {
            "add" => {
                println!("Please input the desc that todo: ");
                let mut desc = String::new();
                io::stdin().read_line(&mut desc).unwrap();
                let desc = desc.trim();
                add_todo(desc);
            }
            "list" => list_todos(),
            "toggle" => {
                println!("请输入待办事项索引：");
                let mut index = String::new();
                io::stdin().read_line(&mut index).unwrap();
                let index = index.trim().parse().unwrap_or(0);
                toggle_todo(index);
            }
            "exit" => break,
            _ => println!("无效命令"),
        }

        save_todos(&todos).unwrap();
    }
}
